package com.xnyzc.lhy.statistics.service.impl.search;

import com.xnyzc.lhy.common.penum.order.EOrderStatus;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.statistics.component.es.JestService;
import com.xnyzc.lhy.statistics.entity.order.OaCOrderPersonal;
import com.xnyzc.lhy.statistics.entity.param.capacity.CapacityParam;
import com.xnyzc.lhy.statistics.mapper.statistics.CollectCapacityStatisticalMapper;
import com.xnyzc.lhy.statistics.service.search.OrderSearchService;
import io.searchbox.client.JestResult;
import io.searchbox.core.Bulk;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.search.aggregation.TermsAggregation;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 订单搜索服务类
 *
 * @author cuixiaojian
 */
@Slf4j
@Service
public class OrderSearchServiceImpl implements OrderSearchService {

    @Autowired
    private JestService restService;

    @Autowired
    private CollectCapacityStatisticalMapper collectCapacityStatisticalMapper;

    /**
     * 单个订单记录保存
     * @param oaCOrderPersonal 订单 entity
     * @return SucStatus 实体
     */
    @Override
    public boolean submitOrder(OaCOrderPersonal oaCOrderPersonal) {

        JestResult jestResult=null;

        // 构造索引
        Index index = new Index.Builder(oaCOrderPersonal).index(OaCOrderPersonal.INDEX_NAME).id(oaCOrderPersonal.getOrderNo()).build();

        jestResult = restService.jestExecute(index);

        return jestResult.isSucceeded();

    }

    /**
     * 大批量订单记录保存
     * @param oaCOrderPersonals 订单 entity List
     * @return SucStatus 实体
     */
    @Override
    public boolean batchesSubmitOrder(List<OaCOrderPersonal> oaCOrderPersonals) {

        JestResult jestResult=null;

        // 建造容器
        Bulk.Builder bulk = new Bulk.Builder();

        for (OaCOrderPersonal entity : oaCOrderPersonals) {
            Index index = new Index.Builder(entity).index(OaCOrderPersonal.INDEX_NAME).id(entity.getOrderNo()).build();
            bulk.addAction(index);
        }

        jestResult = restService.jestExecute(bulk.build());

        return jestResult.isSucceeded();

    }

    /**
     * 批量订单保存接口
     */
    @Override
    public boolean saveOrderToSearch() {

        List<OaCOrderPersonal> oaCOrderPersonals = collectCapacityStatisticalMapper.searchOrder();

        boolean result = this.batchesSubmitOrder(oaCOrderPersonals);

        return result;

    }

    /**
     * 订单编号精准匹配
     * @param orderId 订单ID
     * @return OaCOrderPersonal
     */
    @Override
    public OaCOrderPersonal searchOrder(Long orderId) {

        JestResult jestResult=null;

        // 创建ES检索对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        // 单字段精准匹配
        searchSourceBuilder.query(QueryBuilders.matchQuery("orderNo",orderId));

        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(OaCOrderPersonal.INDEX_NAME).build();

        jestResult = restService.jestExecute(search);

        return jestResult.getSourceAsObject(OaCOrderPersonal.class);
    }

    /**
     * 今日订单流水
     * @return
     */
    @Override
    public Double orderFlowToDay(CapacityParam capacityParam) {

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        // 今日
        restService.addToDay(queryBuilder, OaCOrderPersonal.CREATE_TIME);

        // 状态联合OR查询
        BoolQueryBuilder orderStatusBool = QueryBuilders.boolQuery();
        orderStatusBool.should(QueryBuilders.termQuery(OaCOrderPersonal.ORDER_STATUS, EOrderStatus.paymented.getType()));
        orderStatusBool.should(QueryBuilders.termQuery(OaCOrderPersonal.ORDER_STATUS, EOrderStatus.normalOrderPendingPayment.getType()));

        queryBuilder.must(orderStatusBool);

        // 求和
        SumAggregationBuilder sumAggregationBuilder = AggregationBuilders.sum(OaCOrderPersonal.TOTAL_FEE).field(OaCOrderPersonal.TOTAL_FEE);
        searchSourceBuilder.aggregation(sumAggregationBuilder);

        searchSourceBuilder.query(queryBuilder).fetchSource(new String [] {OaCOrderPersonal.ORDER_STATUS}, null);

        log.info(searchSourceBuilder.toString());

        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(OaCOrderPersonal.INDEX_NAME).build();

        SearchResult result = restService.searchExecute(search);

        List<TermsAggregation.Entry> entries = result.getAggregations().getTermsAggregation(OaCOrderPersonal.TOTAL_FEE).getBuckets();

        Double sum = 0.0;

        for (TermsAggregation.Entry entry : entries) {
            sum = entry.getSumAggregation(OaCOrderPersonal.TOTAL_FEE).getSum();
        }

        return sum;
    }

    /**
     * 今日乘客发单量
     */
    @Override
    public Integer userBillCountToDay(CapacityParam capacityParam) {

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        if(CheckUtil.objIsNotEmpty(capacityParam.getCityId())) {
            queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.CITY_ID, capacityParam.getCityId()));
        }

        // 今日
        restService.addToDay(queryBuilder, OaCOrderPersonal.CREATE_TIME);

        // 求总
        ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count(OaCOrderPersonal.DRAW_FEE).field(OaCOrderPersonal.DRAW_FEE);
        searchSourceBuilder.aggregation(valueCountAggregationBuilder);

        searchSourceBuilder.query(queryBuilder).fetchSource(new String [] {OaCOrderPersonal.DRAW_FEE}, null);

        log.info(searchSourceBuilder.toString());

        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(OaCOrderPersonal.INDEX_NAME).build();

        SearchResult result = restService.searchExecute(search);

        List<TermsAggregation.Entry> entries = result.getAggregations().getTermsAggregation(OaCOrderPersonal.DRAW_FEE).getBuckets();

        long count = 0L;

        for (TermsAggregation.Entry entry : entries) {
            count = entry.getValueCountAggregation(OaCOrderPersonal.DRAW_FEE).getValueCount();
        }

        return Integer.parseInt(String.valueOf(count));
    }

    /**
     * 今日应答订单量
     */
    @Override
    public Integer driverReplyCountToDay(CapacityParam capacityParam) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        // 去接乘客时间不为空
        queryBuilder.must(QueryBuilders.existsQuery(OaCOrderPersonal.MEET_PASSENGER_TIME));

        if(CheckUtil.objIsNotEmpty(capacityParam.getCityId())) {
            queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.CITY_ID, capacityParam.getCityId()));
        }

        // 今日
        restService.addToDay(queryBuilder, OaCOrderPersonal.CREATE_TIME);

        // 求总
        ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count(OaCOrderPersonal.DRAW_FEE).field(OaCOrderPersonal.DRAW_FEE);
        searchSourceBuilder.aggregation(valueCountAggregationBuilder);

        searchSourceBuilder.query(queryBuilder).fetchSource(new String [] {OaCOrderPersonal.DRAW_FEE}, null);

        log.info(searchSourceBuilder.toString());

        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(OaCOrderPersonal.INDEX_NAME).build();

        SearchResult result = restService.searchExecute(search);

        List<TermsAggregation.Entry> entries = result.getAggregations().getTermsAggregation(OaCOrderPersonal.DRAW_FEE).getBuckets();

        long count = 0L;

        for (TermsAggregation.Entry entry : entries) {
            count = entry.getValueCountAggregation(OaCOrderPersonal.DRAW_FEE).getValueCount();
        }

        return Integer.parseInt(String.valueOf(count));
    }

    /**
     * 今日完成订单量
     */
    @Override
    public Integer driverCompleteCountToDay(CapacityParam capacityParam) {

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        if(CheckUtil.objIsNotEmpty(capacityParam.getCityId())) {
            queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.CITY_ID, capacityParam.getCityId()));
        }

        // 今日
        restService.addToDay(queryBuilder, OaCOrderPersonal.CREATE_TIME);

        // 状态联合OR查询
        BoolQueryBuilder orderStatusBool = QueryBuilders.boolQuery();
        orderStatusBool.should(QueryBuilders.termQuery(OaCOrderPersonal.ORDER_STATUS, EOrderStatus.sendBill.getType()));
        orderStatusBool.should(QueryBuilders.termQuery(OaCOrderPersonal.ORDER_STATUS, EOrderStatus.paymented.getType()));
        orderStatusBool.should(QueryBuilders.termQuery(OaCOrderPersonal.ORDER_STATUS, EOrderStatus.normalOrderPendingPayment.getType()));

        queryBuilder.must(orderStatusBool);

        // 求总
        ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count(OaCOrderPersonal.DRAW_FEE).field(OaCOrderPersonal.DRAW_FEE);
        searchSourceBuilder.aggregation(valueCountAggregationBuilder);

        searchSourceBuilder.query(queryBuilder).fetchSource(new String [] {OaCOrderPersonal.DRAW_FEE}, null);

        log.info(searchSourceBuilder.toString());

        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(OaCOrderPersonal.INDEX_NAME).build();

        SearchResult result = restService.searchExecute(search);

        List<TermsAggregation.Entry> entries = result.getAggregations().getTermsAggregation(OaCOrderPersonal.DRAW_FEE).getBuckets();

        Long count = 0L;

        for (TermsAggregation.Entry entry : entries) {
            count = entry.getValueCountAggregation(OaCOrderPersonal.DRAW_FEE).getValueCount();
        }

        return Integer.parseInt(String.valueOf(count));

    }

    /**
     * 应答前乘客其他平台受理订单
     */
    @Override
    public Integer priorOtherAcceptance(CapacityParam capacityParam) {

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        // 司机ID为空
        queryBuilder.mustNot(QueryBuilders.existsQuery(OaCOrderPersonal.DRIVER_ID));

        if(CheckUtil.objIsNotEmpty(capacityParam.getCityId())) {
            queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.CITY_ID, capacityParam.getCityId()));
        }

        queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.ORDER_STATUS, EOrderStatus.otherPlatformsAccept.getType()));

        // 今日
        restService.addToDay(queryBuilder, OaCOrderPersonal.CREATE_TIME);

        // 求总
        ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count(OaCOrderPersonal.DRAW_FEE).field(OaCOrderPersonal.DRAW_FEE);
        searchSourceBuilder.aggregation(valueCountAggregationBuilder);

        searchSourceBuilder.query(queryBuilder).fetchSource(new String [] {OaCOrderPersonal.DRAW_FEE}, null);

        log.info(searchSourceBuilder.toString());

        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(OaCOrderPersonal.INDEX_NAME).build();

        SearchResult result = restService.searchExecute(search);

        List<TermsAggregation.Entry> entries = result.getAggregations().getTermsAggregation(OaCOrderPersonal.DRAW_FEE).getBuckets();

        long count = 0L;

        for (TermsAggregation.Entry entry : entries) {
            count = entry.getValueCountAggregation(OaCOrderPersonal.DRAW_FEE).getValueCount();
        }

        return Integer.parseInt(String.valueOf(count));

    }

    /**
     * 应答后乘客其他平台受理订单
     */
    @Override
    public Integer priorOtherAcceptanceAfter(CapacityParam capacityParam) {

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        // 司机ID不为空
        queryBuilder.must(QueryBuilders.existsQuery(OaCOrderPersonal.DRIVER_ID));

        if(CheckUtil.objIsNotEmpty(capacityParam.getCityId())) {
            queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.CITY_ID, capacityParam.getCityId()));
        }

        queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.ORDER_STATUS, EOrderStatus.otherPlatformsAccept.getType()));

        // 今日
        restService.addToDay(queryBuilder, OaCOrderPersonal.CREATE_TIME);

        // 求总
        ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count(OaCOrderPersonal.DRAW_FEE).field(OaCOrderPersonal.DRAW_FEE);
        searchSourceBuilder.aggregation(valueCountAggregationBuilder);

        searchSourceBuilder.query(queryBuilder).fetchSource(new String [] {OaCOrderPersonal.DRAW_FEE}, null);

        log.info(searchSourceBuilder.toString());

        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(OaCOrderPersonal.INDEX_NAME).build();

        SearchResult result = restService.searchExecute(search);

        List<TermsAggregation.Entry> entries = result.getAggregations().getTermsAggregation(OaCOrderPersonal.DRAW_FEE).getBuckets();

        long count = 0L;

        for (TermsAggregation.Entry entry : entries) {
            count = entry.getValueCountAggregation(OaCOrderPersonal.DRAW_FEE).getValueCount();
        }

        return Integer.parseInt(String.valueOf(count));

    }

    /**
     * 调度超时订单
     */
    @Override
    public Integer getOrderSummaryByOrderStatus(CapacityParam capacityParam) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        if(CheckUtil.objIsNotEmpty(capacityParam.getCityId())) {
            queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.CITY_ID, capacityParam.getCityId()));
        }

        queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.ORDER_STATUS, EOrderStatus.orderTimeout.getType()));

        // 今日
        restService.addToDay(queryBuilder, OaCOrderPersonal.CREATE_TIME);

        // 求总
        ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count(OaCOrderPersonal.DRAW_FEE).field(OaCOrderPersonal.DRAW_FEE);
        searchSourceBuilder.aggregation(valueCountAggregationBuilder);

        searchSourceBuilder.query(queryBuilder).fetchSource(new String [] {OaCOrderPersonal.DRAW_FEE}, null);

        log.info(searchSourceBuilder.toString());

        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(OaCOrderPersonal.INDEX_NAME).build();

        SearchResult result = restService.searchExecute(search);

        List<TermsAggregation.Entry> entries = result.getAggregations().getTermsAggregation(OaCOrderPersonal.DRAW_FEE).getBuckets();

        long count = 0L;

        for (TermsAggregation.Entry entry : entries) {
            count = entry.getValueCountAggregation(OaCOrderPersonal.DRAW_FEE).getValueCount();
        }

        return Integer.parseInt(String.valueOf(count));
    }

    /**
     * 应答前乘客取消量
     */
    @Override
    public Integer afterCancellation(CapacityParam capacityParam) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        if(CheckUtil.objIsNotEmpty(capacityParam.getCityId())) {
            queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.CITY_ID, capacityParam.getCityId()));
        }

        queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.ORDER_STATUS, EOrderStatus.cancelOrdersFeesPaid.getType()));

        // 今日
        restService.addToDay(queryBuilder, OaCOrderPersonal.CREATE_TIME);

        // 求总
        ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count(OaCOrderPersonal.DRAW_FEE).field(OaCOrderPersonal.DRAW_FEE);
        searchSourceBuilder.aggregation(valueCountAggregationBuilder);

        searchSourceBuilder.query(queryBuilder).fetchSource(new String [] {OaCOrderPersonal.DRAW_FEE}, null);

        log.info(searchSourceBuilder.toString());

        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(OaCOrderPersonal.INDEX_NAME).build();

        SearchResult result = restService.searchExecute(search);

        List<TermsAggregation.Entry> entries = result.getAggregations().getTermsAggregation(OaCOrderPersonal.DRAW_FEE).getBuckets();

        long count = 0L;

        for (TermsAggregation.Entry entry : entries) {
            count = entry.getValueCountAggregation(OaCOrderPersonal.DRAW_FEE).getValueCount();
        }

        return Integer.parseInt(String.valueOf(count));
    }

    /**
     * 应答后司机取消订单量
     */
    @Override
    public Integer afterDriverCancel(CapacityParam capacityParam) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        if(CheckUtil.objIsNotEmpty(capacityParam.getCityId())) {
            queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.CITY_ID, capacityParam.getCityId()));
        }

        queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.ORDER_STATUS, EOrderStatus.driverCancelOrder.getType()));

        // 今日
        restService.addToDay(queryBuilder, OaCOrderPersonal.CREATE_TIME);

        // 求总
        ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count(OaCOrderPersonal.DRAW_FEE).field(OaCOrderPersonal.DRAW_FEE);
        searchSourceBuilder.aggregation(valueCountAggregationBuilder);

        searchSourceBuilder.query(queryBuilder).fetchSource(new String [] {OaCOrderPersonal.DRAW_FEE}, null);

        log.info(searchSourceBuilder.toString());

        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(OaCOrderPersonal.INDEX_NAME).build();

        SearchResult result = restService.searchExecute(search);

        List<TermsAggregation.Entry> entries = result.getAggregations().getTermsAggregation(OaCOrderPersonal.DRAW_FEE).getBuckets();

        long count = 0L;

        for (TermsAggregation.Entry entry : entries) {
            count = entry.getValueCountAggregation(OaCOrderPersonal.DRAW_FEE).getValueCount();
        }

        return Integer.parseInt(String.valueOf(count));
    }

    /**
     * 应答后乘客取消订单量
     */
    @Override
    public Integer rearPassengerCancellation(CapacityParam capacityParam) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        if(CheckUtil.objIsNotEmpty(capacityParam.getCityId())) {
            queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.CITY_ID, capacityParam.getCityId()));
        }

        queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.ORDER_STATUS, EOrderStatus.cancelOrderNoNeedPayment.getType()));

        // 今日
        restService.addToDay(queryBuilder, OaCOrderPersonal.CREATE_TIME);

        // 求总
        ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count(OaCOrderPersonal.DRAW_FEE).field(OaCOrderPersonal.DRAW_FEE);
        searchSourceBuilder.aggregation(valueCountAggregationBuilder);

        searchSourceBuilder.query(queryBuilder).fetchSource(new String [] {OaCOrderPersonal.DRAW_FEE}, null);

        log.info(searchSourceBuilder.toString());

        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(OaCOrderPersonal.INDEX_NAME).build();

        SearchResult result = restService.searchExecute(search);

        List<TermsAggregation.Entry> entries = result.getAggregations().getTermsAggregation(OaCOrderPersonal.DRAW_FEE).getBuckets();

        long count = 0L;

        for (TermsAggregation.Entry entry : entries) {
            count = entry.getValueCountAggregation(OaCOrderPersonal.DRAW_FEE).getValueCount();
        }

        return Integer.parseInt(String.valueOf(count));
    }

    /**
     * 客服取消订单量
     */
    @Override
    public Integer serviceCancellation(CapacityParam capacityParam) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        if(CheckUtil.objIsNotEmpty(capacityParam.getCityId())) {
            queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.CITY_ID, capacityParam.getCityId()));
        }

        queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.ORDER_STATUS, EOrderStatus.reassigned.getType()));

        // 今日
        restService.addToDay(queryBuilder, OaCOrderPersonal.CREATE_TIME);

        // 求总
        ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count(OaCOrderPersonal.DRAW_FEE).field(OaCOrderPersonal.DRAW_FEE);
        searchSourceBuilder.aggregation(valueCountAggregationBuilder);

        searchSourceBuilder.query(queryBuilder).fetchSource(new String [] {OaCOrderPersonal.DRAW_FEE}, null);

        log.info(searchSourceBuilder.toString());

        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(OaCOrderPersonal.INDEX_NAME).build();

        SearchResult result = restService.searchExecute(search);

        List<TermsAggregation.Entry> entries = result.getAggregations().getTermsAggregation(OaCOrderPersonal.DRAW_FEE).getBuckets();

        long count = 0L;

        for (TermsAggregation.Entry entry : entries) {
            count = entry.getValueCountAggregation(OaCOrderPersonal.DRAW_FEE).getValueCount();
        }

        return Integer.parseInt(String.valueOf(count));
    }

    /**
     * 履行中订单量
     */
    @Override
    public Integer inPerformance(CapacityParam capacityParam) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        if(CheckUtil.objIsNotEmpty(capacityParam.getCityId())) {
            queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.CITY_ID, capacityParam.getCityId()));
        }

        queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.ORDER_STATUS, EOrderStatus.duringTrip.getType()));

        // 今日
        restService.addToDay(queryBuilder, OaCOrderPersonal.CREATE_TIME);

        // 求总
        ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count(OaCOrderPersonal.DRAW_FEE).field(OaCOrderPersonal.DRAW_FEE);
        searchSourceBuilder.aggregation(valueCountAggregationBuilder);

        searchSourceBuilder.query(queryBuilder).fetchSource(new String [] {OaCOrderPersonal.DRAW_FEE}, null);

        log.info(searchSourceBuilder.toString());

        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(OaCOrderPersonal.INDEX_NAME).build();

        SearchResult result = restService.searchExecute(search);

        List<TermsAggregation.Entry> entries = result.getAggregations().getTermsAggregation(OaCOrderPersonal.DRAW_FEE).getBuckets();

        long count = 0L;

        for (TermsAggregation.Entry entry : entries) {
            count = entry.getValueCountAggregation(OaCOrderPersonal.DRAW_FEE).getValueCount();
        }

        return Integer.parseInt(String.valueOf(count));
    }

    /**
     * 待支付订单量
     */
    @Override
    public Integer toBePaid(CapacityParam capacityParam) {

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        if(CheckUtil.objIsNotEmpty(capacityParam.getCityId())) {
            queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.CITY_ID, capacityParam.getCityId()));
        }

        // 今日
        restService.addToDay(queryBuilder, OaCOrderPersonal.CREATE_TIME);

        // 状态联合OR查询
        BoolQueryBuilder orderStatusBool = QueryBuilders.boolQuery();
        orderStatusBool.should(QueryBuilders.termQuery(OaCOrderPersonal.ORDER_STATUS, EOrderStatus.normalOrderPendingPayment.getType()));
        orderStatusBool.should(QueryBuilders.termQuery(OaCOrderPersonal.ORDER_STATUS, EOrderStatus.cancelPendingPayment.getType()));

        queryBuilder.must(orderStatusBool);

        // 求总
        ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count(OaCOrderPersonal.DRAW_FEE).field(OaCOrderPersonal.DRAW_FEE);
        searchSourceBuilder.aggregation(valueCountAggregationBuilder);

        searchSourceBuilder.query(queryBuilder).fetchSource(new String [] {OaCOrderPersonal.DRAW_FEE}, null);

        log.info(searchSourceBuilder.toString());

        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(OaCOrderPersonal.INDEX_NAME).build();

        SearchResult result = restService.searchExecute(search);

        List<TermsAggregation.Entry> entries = result.getAggregations().getTermsAggregation(OaCOrderPersonal.DRAW_FEE).getBuckets();

        Long count = 0L;

        for (TermsAggregation.Entry entry : entries) {
            count = entry.getValueCountAggregation(OaCOrderPersonal.DRAW_FEE).getValueCount();
        }

        return Integer.parseInt(String.valueOf(count));

    }

    /**
     * 已支付订单量
     */
    @Override
    public Integer paidPassengerCancellation(CapacityParam capacityParam) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        if(CheckUtil.objIsNotEmpty(capacityParam.getCityId())) {
            queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.CITY_ID, capacityParam.getCityId()));
        }

        queryBuilder.must(QueryBuilders.matchQuery(OaCOrderPersonal.ORDER_STATUS, EOrderStatus.paymented.getType()));

        // 今日
        restService.addToDay(queryBuilder, OaCOrderPersonal.CREATE_TIME);

        // 求总
        ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count(OaCOrderPersonal.DRAW_FEE).field(OaCOrderPersonal.DRAW_FEE);
        searchSourceBuilder.aggregation(valueCountAggregationBuilder);

        searchSourceBuilder.query(queryBuilder).fetchSource(new String [] {OaCOrderPersonal.DRAW_FEE}, null);

        log.info(searchSourceBuilder.toString());

        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(OaCOrderPersonal.INDEX_NAME).build();

        SearchResult result = restService.searchExecute(search);

        List<TermsAggregation.Entry> entries = result.getAggregations().getTermsAggregation(OaCOrderPersonal.DRAW_FEE).getBuckets();

        long count = 0L;

        for (TermsAggregation.Entry entry : entries) {
            count = entry.getValueCountAggregation(OaCOrderPersonal.DRAW_FEE).getValueCount();
        }

        return Integer.parseInt(String.valueOf(count));
    }

}
